//2 global vars used for the ajax request
var url;
var jsonQueryData;

function getAjaxData(keyword) {
    //1) we prepare the JSON request's content
    prepareJSON(keyword);
    
    //2) we fire the request, passing it the name of our callback in case of a success
    sendRequestToServer(onRequestSuccess);
}

function prepareJSON(keyword) {
    url="http://localhost/markMyWeb/ajax/bookmarks.php"; 
    
    jsonQueryData={
        "action":"read",
        "fields":"ta,ti,u",
        "crit":"ta",
        "key":keyword
    };    
}

/** Format of the result :
 *  arReturnParameter: {
        default: [
            {
            tag: "dummyfortest1"
            title: ""
            url: "www.doesntexist.com"
            }
            {
            tag: "test"
            title: "Google"
            url: "http://www.google.com/"
            }
            ...
 */
function onRequestSuccess(data) {
    var aTag=null;
    var aWebpage=null;
    
    var currentTag="";
    var currentWebpageTitle="";
    var currentWebpageUrl="";
    
    var j;
    var k;
    
    var arReturnParameter=$.parseJSON(data).arReturnParameter["default"];
    
    //    alert(arReturnParameter[0]["tag"]);
    arTags=new Array();
    j=0;
    k=0;
    
    for (var i=0;i<arReturnParameter.length;i++) {
    //we browse through the tags
        currentWebpageTitle=arReturnParameter[i].title;
        currentWebpageUrl=arReturnParameter[i].url;
        
        if (currentTag!=arReturnParameter[i].tag) {
        //this is a new Tag
            if (aTag!=null) {
                //this is not the first iteration: we store the previous tag
                arTags[j]=aTag;
                j++;
            }                                   
            
            currentTag=arReturnParameter[i].tag;
            
            //we prepare the vars for the new tag
            aTag=new Object();
            aTag.tag=currentTag;
            aTag.arWebpages=new Array();
            k=0;
        }
        
        aWebpage=new Object;
        aWebpage.title=currentWebpageTitle;
        aWebpage.url=currentWebpageUrl;
        aTag.arWebpages[k]=aWebpage;
        k++;
    }
    
    if (aTag!=null) {
    //we store the last tag
        arTags[j]=aTag;
    }
        
    buildTree();
}
/**
* Makes the actual call to the markMyWeb server 
* METHOD: POST
* FORMAT: JSON
*/
function sendRequestToServer(callback) {
$.post(
//$.getJSON(
url,            
jsonQueryData
)
.success(function(data) {
    console.log("ajax-data.js sendRequestToServer : Ajax query successful");
    console.log("ajax-data.js sendRequestToServer : Retrieved data:"+data);
        
    callback(data);
})
.error(function(data) {
    console.log("ajax-data.js sendRequestToServer : Ajax query error!");
    console.log("ajax-data.js sendRequestToServer : failed with status "+data.statusText+"********"+data.status+"********"+data);
});
//    .success(function(jsonQueryData) { callbackSuccess(jsonQueryData)});
//    .success(function(data) { callbackSuccess(data)})
//    .error(callbackError(data));
}
